package com.zbcbms.controller;

import com.alibaba.druid.util.StringUtils;
import com.zbcbms.common.AlertUtilCommon;
import com.zbcbms.entity.dto.AddBookBorrowDto;
import com.zbcbms.entity.dto.UserMap;
import com.zbcbms.service.BookService;
import com.zbcbms.service.UserService;
import com.zbcbms.service.impl.BookBorrowServiceImpl;
import com.zbcbms.service.impl.BookServiceImpl;
import com.zbcbms.service.impl.UserServiceImpl;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import java.net.URL;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author fengq
 */
public class AddBorrowController implements Initializable {

    @FXML
    private ComboBox<String> addBookNumber;
    @FXML
    private ComboBox<String> addUserNumber;
    @FXML
    private ComboBox<String> borrowDays;
    @FXML
    private TextField addUserName;
    @FXML
    private TextField addBookName;
    private UserService userService=new UserServiceImpl();
    private BookService bookService=new BookServiceImpl();
    private AlertUtilCommon alertUtilCommon=new AlertUtilCommon();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        List<String> nums = Arrays.asList("5","7", "14","30");
        ObservableList<String> obList = FXCollections.observableArrayList(nums);
        borrowDays.setItems(obList);
        borrowDays.getSelectionModel().select(0);
        addUserNumber.setItems(userService.getAllUserNumber());
        addBookNumber.setItems(bookService.getAllBookNumberWithStatus());
    }

    @FXML
    private void addBorrowAction(){
        String userNumber = addUserNumber.getEditor().getText();
        String bookNumber =addBookNumber.getEditor().getText();
        String addBorrowDays=borrowDays.getEditor().getText();
        if (StringUtils.isEmpty(userNumber)||StringUtils.isEmpty(bookNumber)||StringUtils.isEmpty(addBorrowDays)){
            alertUtilCommon.AlterMessageInform(addBookNumber.getScene().getWindow(), Alert.AlertType.ERROR,"错误提示",
                    "参数不能空","小仲书：差忆点点成功");
        }else{
            AddBookBorrowDto addBookBorrowDto = new AddBookBorrowDto();
            addBookBorrowDto.setBookNumber(bookNumber);
            addBookBorrowDto.setUserNumber(userNumber);
            addBookBorrowDto.setOperatorName(UserMap.getName());
            addBookBorrowDto.setBorrowDay(addBorrowDays);
            addBookBorrowDto.setBorrowDate(new Date());
            Boolean aBoolean = new BookBorrowServiceImpl().saveOne(addBookBorrowDto);
            if (aBoolean){
                alertUtilCommon.AlterMessageInform(addBookNumber.getScene().getWindow(), Alert.AlertType.INFORMATION,"成功提示",
                        "操作成功","小仲书：借书成功，记得准时还书哟！！");
                resetBorrowTextAction();
                String keyword = addBookNumber.getEditor().getText();
                addBookNumber.setItems(bookService.getBookNumberByKeywordWithStatus(keyword));
            }else {
                alertUtilCommon.AlterMessageInform(addBookNumber.getScene().getWindow(), Alert.AlertType.ERROR,"错误提示",
                        "添加失败","小仲书：差亿点点成功");
            }

        }

    }

    @FXML
    private void resetBorrowTextAction(){
        addBookNumber.getEditor().setText("");
        addUserNumber.getEditor().setText("");
        borrowDays.getSelectionModel().select(0);
        addUserName.setText("");
        addBookName.setText("");
    }

    @FXML
    private void bookNumberReleased(){
        String keyword = addBookNumber.getEditor().getText();
        addBookNumber.setItems(bookService.getBookNumberByKeywordWithStatus(keyword));
        addBookNumber.show();
    }

    @FXML
    private void userNumberReleased(){
        String keyword = addUserNumber.getEditor().getText();
        addUserNumber.setItems(userService.getUserNumberByKeyword(keyword));
        addUserNumber.show();
    }

    @FXML
    public void addUserNumberAction(){
        addUserName.setText(userService.getUserNameByNumber(addUserNumber.getEditor().getText()));
    }

    @FXML
    public void addBookNumberAction(){
        addBookName.setText(bookService.getBookNameByNumber(addBookNumber.getEditor().getText()));

    }


}
